<template>
  <div  class="infobox">
    <div class="topbox">
      <el-button type="primary" @click="addbutton">添加化验单</el-button>
    </div>
    <div>
      <el-table :data="datalist" border style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="date" label="添加化验单" width="120" show-overflow-tooltip />
        <el-table-column prop="name" label="检查项" width="120" />
        <el-table-column prop="name" label="项目" width="120" />
        <el-table-column prop="name" label="结果" width="120" />
        <el-table-column prop="name" label="状态" width="120" />
        <el-table-column prop="name" label="单位" width="120" />
        <el-table-column prop="name" label="化验时间" width="120" />
        <el-table-column prop="name" label="化验医院" width="120" />
        <el-table-column prop="name" label="记录人" width="120" />
        <el-table-column label="address">
          <template #default="scope">{{ scope.row.address }}</template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog v-model="dialogVisible" title="添加化验单" width="800" :before-close="handleClose">

      <el-form :model="formlist" label-width="auto" style="max-width: 700px">
        <el-form-item label="化验照片">
          <el-upload v-model:file-list="fileList" class="upload-demo" multiple :limit="1" :http-request="uploadFile">
            <el-button type="primary">上传图片</el-button>
            <template #tip>
              <div class="el-upload__tip">
              </div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="化验时间">
          <el-date-picker v-model="formlist.date1" value-format="yyyy-MM-dd" type="date" style="width: 240px" />
        </el-form-item>
        <el-form-item label="化验医院">
          <el-input v-model="formlist.name" />
        </el-form-item>
        <el-form-item label="化验项目">
          <el-select v-model="formlist.hyxm" multiple placeholder="Select" style="width: 100%" @change="hyxulist">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-table v-if="showtable" :data="tableData" border style="width: 100%" :span-method="objectSpanMethod">
          <el-table-column prop="hyxm" label="化验项目" width="120" />
          <el-table-column prop="xm" label="项目" width="120" />
          <el-table-column label="结果" width="120">
            <template #default="scope">
              <div style="display: flex; align-items: center">
                <el-input v-model="scope.row.jg"></el-input>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="状态" width="120">
            <template #default="scope">
              <div style="display: flex; align-items: center">
                <el-select v-model="scope.row.zt" placeholder="Select" size="large" style="width: 240px">
                  <el-option v-for="item in scope.row.ztlist" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="dw" label="单位" width="100" />
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="savebutton">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
let datalist = ref([
  {
    date: "2016-05-03",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  },
  {
    date: "2016-05-02",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  },
  {
    date: "2016-05-04",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  },
  {
    date: "2016-05-01",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  }
]);
const options = [
  {
    value: '1',//化验项目的id
    label: '血糖', //名称
  },
  {
    value: '2',//化验项目的id
    label: '血脂四项', //名称
  },
  {
    value: '3',//化验项目的id
    label: '肾功能', //名称
  },
  {
    value: '4',//化验项目的id
    label: '肝功能', //名称
  }
]
let bighyxmlist = [
  {
    id: 1, //化验项目的id
    name: "血糖", //名称
    tablelsit: [
      {
        hyxm: '血糖',//化验项目
        xm: '餐后2H指血血糖',//项目
        jg: 1,//结果
        zt: 1, //状态
        dw: 'mmol/L', //单位
        ztlist: [{
          value: 1,
          label: '↑',
        },
        {
          value: 2,
          label: '↓',
        },
        {
          value: 3,
          label: '正常',
        },
        {
          value: 4,
          label: '空',
        },
        ],//状态的下拉阈值
      },
      {
        hyxm: '血糖',
        xm: '餐后2H静脉血血糖',//项目
        jg: '',//结果
        zt: '', //状态
        dw: 'mmol/L',
        ztlist: [{
          value: 1,
          label: '↑',
        },
        {
          value: 2,
          label: '↓',
        },
        {
          value: 3,
          label: '正常',
        },
        {
          value: 4,
          label: '空',
        },
        ],//状态的下拉阈值
      },
    ]
  },
  {
    id: 2,
    name: "血脂四项",
    tablelsit: [
      {
        hyxm: '血脂四项',
        xm: '血脂1',//项目
        jg: 1,//结果
        zt: 1, //状态
        dw: 'mmol/L',
        ztlist: [{
          value: 1,
          label: '↑',
        },
        {
          value: 2,
          label: '↓',
        },
        {
          value: 3,
          label: '正常',
        },
        {
          value: 4,
          label: '空',
        },
        ],//状态的下拉阈值
      },
      {
        hyxm: '血脂四项',
        xm: '血脂2',//项目
        jg: '',//结果
        zt: '', //状态
        dw: 'mmol/L',
        ztlist: [{
          value: 1,
          label: '↑',
        },
        {
          value: 2,
          label: '↓',
        },
        {
          value: 3,
          label: '正常',
        },
        {
          value: 4,
          label: '空',
        },
        ],//状态的下拉阈值
      },
    ]
  },
]
const tableData = ref([]);
let showtable=ref(false)
let addbutton = () => {
  dialogVisible.value = true
}
const dialogVisible = ref(false)

const handleClose = () => {
  dialogVisible.value = false
}
const fileList = ref([])
let uploadFile = (val) => {
  const fd = new FormData()
  fd.append('functionId', val.file.uid)
  fd.append('moduleId', 19)
  fd.append('upfile', val.file)
}
let formlist = ref({})
let savebutton = () => {
  console.log(formlist.value)
}
const spanArr = ref([]);
const pos = ref(0);
const getSpanArr = (data) => {
  spanArr.value = [];
  for (let i = 0; i < data.length; i++) {
    if (i === 0) {
      spanArr.value.push(1);
      pos.value = 0;
    } else {
      // 判断当前元素与上一个元素是否相同
      if (data[i].hyxm === data[i - 1].hyxm) {
        spanArr.value[pos.value] += 1;
        spanArr.value.push(0);
      } else {
        spanArr.value.push(1);
        pos.value = i;
      }
    }
  }
}

// 合并方法
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    const _row = spanArr.value[rowIndex];
    const _col = _row > 0 ? 1 : 0;
    return {
      rowspan: _row,
      colspan: _col
    };
  }
};
//下拉化验项目单子,选出对用的项目
let hyxulist = (e) => {
  console.log('w=======>,', e)
  let key=e
  let mergedTableList = [];

  key.forEach(k => {
    const item = bighyxmlist.find(item => item.id.toString() === k);
    if (item) {
      mergedTableList = mergedTableList.concat(item.tablelsit);
    }
  });
  tableData.value=mergedTableList
  showtable.value=true
  getSpanArr(tableData.value);
  console.log("mergedTableList=======>",mergedTableList)
}


</script>
<style lang='scss' scoped>
.topbox {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
.tableboxs{
  height: 500px;
  overflow: auto;
}

.hydbox {
  display: flex;
  align-items: center;
}
.infobox {
  height: 400px;
  overflow: auto;
}
</style>